package com.nbsaas.boot.controller.article;

import com.nbsaas.boot.rest.annotations.*;
import com.nbsaas.boot.rest.response.ListResponse;
import com.nbsaas.boot.rest.response.PageResponse;
import com.nbsaas.boot.rest.response.ResponseObject;
import com.nbsaas.boot.article.api.domain.request.ArticleRequest;
import com.nbsaas.boot.article.api.domain.request.ArticleSearch;
import com.nbsaas.boot.article.api.domain.response.ArticleResponse;
import com.nbsaas.boot.article.api.domain.simple.ArticleSimple;
import com.nbsaas.boot.article.api.apis.ArticleApi;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestBody;
import jakarta.annotation.Resource;

/**
*  控制器
*/
@RequiresAuthentication
@RestController
@RequestMapping("/article")
public class ArticleController {


    @Resource
    private ArticleApi articleApi;

    /**
    * 搜索数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("article")
    @PostMapping("/search")
    public PageResponse <ArticleSimple> search(@RequestBody ArticleSearch request) {
        return articleApi.search(request);
    }

    /**
    * 列表数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("article")
    @PostMapping("/list")
    public ListResponse<ArticleSimple> list(@RequestBody ArticleSearch request) {
        return articleApi.list(request);
    }

    /**
    * 添加数据
    *
    * @param request
    * @return
    */
    @RequiresPermissions("article")
    @CreateData
    @PostMapping("/create")
    public ResponseObject <ArticleResponse> create(@RequestBody @Validated(AddOperator.class) ArticleRequest request) {
        return articleApi.create(request);
    }

   /**
   * 修改数据
   * @param request
   * @return
   */
   @RequiresPermissions("article")
   @UpdateData
   @PostMapping("/update")
   public ResponseObject<ArticleResponse> update(@RequestBody @Validated(UpdateOperator.class) ArticleRequest request) {
       return articleApi.update(request);
   }

    /**
    * 删除数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("article")
    @PostMapping("/delete")
    public ResponseObject<?> delete(@RequestBody @Validated(DeleteOperator.class) ArticleRequest request) {
        return articleApi.delete(request);
    }

    /**
    * 查看数据
    *
    * @param request
    * @return
    */
    @SearchData
    @RequiresPermissions("article")
    @PostMapping("/view")
    public ResponseObject <ArticleResponse> view(@RequestBody @Validated(ViewOperator.class) ArticleRequest  request) {
        return articleApi.view(request);
    }
}